// pages/add-receiveingAdress/add-receiveingAdress.js
const util = require('../../utils/util.js');
import api from '../../utils/request/api'
import { addressList,cooperation } from '../../utils/request/apiurl'


Page({
  data: {
	uid:'',
	adressList:{
		relationName:'',//收货人
		relationTel:'',//收货人电话
		cooperationNetwork:'',//合作网点
		cooperationTel:'',//联系电话
		networkAdress:'',//网点地址
		adressDetail:'',//详细地址
	},
	id:'',//收货地址id
	cooperationId:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	// console.log(options,'optionsoptions')
	var userInfo = wx.getStorageSync('userInfo');
	this.setData({
		uid:userInfo.uid,
		id:options.tid,
		// cooperationId:options.cooperId,
		// cooperationName:options.cooperationName,
		// linkPhone:options.linkPhone
	})
	// console.log(options.relationName)
	if(options.relationName==''||options.relationName==undefined||options.relationName==null){
		this.setData({
			uid:userInfo.uid,
			id:options.tid,
		})
		this.updateAdress();	  
	}else{
		this.setData({
			uid:userInfo.uid,
			id:options.tid,
			cooperationId:options.cooperId,
			'adressList.relationName':options.relationName,
			'adressList.relationTel':options.linkPhone,
		})
		this.cooperationIdList()
	}
	
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	this.cooperationIdList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
	wx.showNavigationBarLoading() //在标题栏中显示加载
	this.cooperationIdList()
	setTimeout(function() {
		wx.hideNavigationBarLoading() //完成停止加载
		wx.stopPullDownRefresh() //停止下拉刷新
	}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 收货地址详情
  updateAdress(){
	  var source = {
	  	id:this.data.id
	  }
	  api.post(addressList.addressDetail, source).then(res => {
		  // console.log(res)
		this.setData({
			'adressList.relationName':res.takePerson,
			'adressList.relationTel':res.takePersonPhone,
			cooperationId:res.cooperationId
		})
		this.cooperationIdList()
	  }).catch(err => {
	  	// console.log(err)
	  	wx.showToast({
	  		title: err.message,
	  		icon: 'none'
	  	})
	  })
  },
  cooperationIdList(){
	 var source = {
	 	id:this.data.cooperationId
	 }
	 api.post(cooperation.getCooperationById, source).then(res => {
		 // console.log(res)
	 		this.setData({
	 			'adressList.cooperationNetwork':res.cooperationName,
	 			'adressList.cooperationTel':res.linkPhone,
	 			'adressList.networkAdress':res.cooperationAddress
	 		})
	 }).catch(err => {
		 // console.log(err)
	 	// wx.showToast({
	 	// 	title: err.message,
	 	// 	icon: 'none'
	 	// })
	 }) 
  },
  
  //修改地址
  addNewAddress(){
	  if(this.data.adressList.relationName==''){
		  wx.showToast({
		  	title: '收货人不能为空',
		  	icon: 'none'
		  })
		  return false
	  }
	  if(this.data.adressList.relationTel==''){
	  		  wx.showToast({
	  		  	title: '收货人电话不能为空',
	  		  	icon: 'none'
	  		  })
			  return false
	  }
	  if(this.data.adressList.cooperationNetwork==''){
	  		  wx.showToast({
	  		  	title: '合作网点不能为空',
	  		  	icon: 'none'
	  		  })
			  return false
	  }
	  var source = {
	  	id: this.data.id,
	  	takePerson: this.data.adressList.relationName,
	  	takePersonPhone: this.data.adressList.relationTel,
	  	cooperationId: this.data.cooperationId,
	  }
	  api.post(addressList.updateAdress, source).then(res => {
		  // console.log(res)
	  	if(res>0){
			wx.navigateBack({
			  delta:1
			})
	  		// wx.navigateTo({
	  		// 	url: '../receiveing-adress/receiveing-adress?tid='+this.data.id
	  		// })
	  	}
	  }).catch(err => {
	  	// console.log(err)
	  	wx.showToast({
	  		title: err.message,
	  		icon: 'none'
	  	})
	  })
  },
  cooperationNetwork(){
	  wx.navigateTo({
	  	url: '../cooperation-network/cooperation-network?tid='+this.data.id
	  })
	  // wx.navigateTo({
	  // 	url: '../cooperation-network/cooperation-network?tid='+this.data.id+'&relationName='+this.data.adressList.relationName+"&linkPhone="+this.data.adressList.relationTel
	  // })
  },
  bindRelationName(e){
	  // console.log(e)
  	this.setData({
  		'adressList.relationName':e.detail.value
  	})
  },
  bindRelationTel(e){
	  // console.log(e)
  	this.setData({
  		'adressList.relationTel':e.detail.value
  	})
  }
})